package com.h.handler;

import com.h.annotation.Handler;
import com.h.autoconfigure.NettyConfigProperties;
import com.h.config.ChannelFactory;
import io.netty.channel.*;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.*;
import io.netty.handler.codec.http.websocketx.*;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * @author ：Hukaiwen
 * @description：
 * @date ：2022/2/22 15:32
 */
@Handler("myhandler4")
public class MyHandler4 extends ChannelInitializer<SocketChannel> {

    @Resource
    private ChannelFactory channelFactory;

    @Resource
    private WebSocketServerHandler webSocketServerHandler;

    @Resource
    private HttpRequestHandler httpRequestHandler;

    private NettyConfigProperties.Configuration configuration;

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        configuration = channelFactory.getConfigs().get(MyHandler4.class);
        ch.pipeline()
                .addLast(new LoggingHandler())
                .addLast(new HttpServerCodec())// websocket 基于http协议，所以要有http编解码器 服务端用HttpServerCodec
                .addLast(new HttpObjectAggregator(65535))// 聚合HTTP请求和响应
                .addLast(new ChunkedWriteHandler())// 支持大对象的写入
                .addLast(httpRequestHandler)
//                .addLast(new WebSocketServerProtocolHandler(configuration.getLocalAddress().getPath()))// WebSocket握手处理器
                .addLast(webSocketServerHandler);
    }
}
